﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace SharePointUsers
{
    public partial class Editor : Form
    {
        public HashSet<string> PropertiesDisplayNames = null;
        public bool HasToBeUnique = false;
        public bool ValueChanged = false;
        public bool NameChanged = false;
        public string OriginalValue = "";
        public string OriginalName = "";
        public bool AddMode = false;
        public Editor()
        {
            InitializeComponent();
        }

        private void buttonOK_Click(object sender, EventArgs e)
        {
            if (AddMode)
            {
                if (textBoxName.Text != "")
                {
                    if (textBoxValue.Text == "")
                    {
                        textBoxValue.Focus();
                    }
                }
            }
            
            if (ValueChanged)
            {
                if (HasToBeUnique)
                {
                    if (Reporter.IsPropertyDisplayNameUniqueAndValid(PropertiesDisplayNames, textBoxValue.Text))
                    {
                        DialogResult = System.Windows.Forms.DialogResult.OK;
                        this.Close();
                    }
                    else
                    {
                        string MessageAdd = "Unable to add this property. The property display name is already in use. Keep the property name but change the display name.";
                        string MessageUpdate = "The new property value (display name) is already in use! This value has to be unique.";
                        if(AddMode)
                            MessageBox.Show(MessageAdd, "Add\\Update property", MessageBoxButtons.OK, MessageBoxIcon.Error);
                        else
                            MessageBox.Show(MessageUpdate, "Add\\Update property", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    }
                }
                else
                {
                    DialogResult = System.Windows.Forms.DialogResult.OK;
                    this.Close();
                }
            }
            else if(NameChanged)
            {
                DialogResult = System.Windows.Forms.DialogResult.OK;
                this.Close();
            }
            else// as if the user clicked cancel
            {
                DialogResult = System.Windows.Forms.DialogResult.Cancel;
                this.Close();
            }
        }

        private void textBoxValue_TextChanged(object sender, EventArgs e)
        {
            if (textBoxValue.Text != OriginalValue)
                ValueChanged = true;
            else
                ValueChanged = false;
        }

        private void textBoxName_TextChanged(object sender, EventArgs e)
        {
            if (textBoxName.Text != OriginalName)
                NameChanged = true;
            else
                NameChanged = false;
        }

        private void textBoxValue_Enter(object sender, EventArgs e)
        {
            if (AddMode && textBoxValue.Text == "")
                textBoxValue.Text = textBoxName.Text;
        }
    }
}
